package com.demo.controller;

import com.demo.dao.FMapper;
import com.demo.dao.GMapper;
import com.demo.domain.Dog;
import com.demo.mapper.EMapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@RestController
public class FriendController {

    @Autowired
    private EMapper eMapper;

    @Autowired
    private FMapper fMapper;

    @Autowired
    private GMapper gMapper;

    @RequestMapping("/c1")
    public List<Map<String, Object>> c1(){
        fMapper.addA("培真");
        fMapper.addA("培真");
        fMapper.addA("培真");
        fMapper.list();
        gMapper.list();

        return eMapper.list();
    }

    @Autowired
    private DataSource dataSource;

    @RequestMapping("/msg")
    public void msg(){
        FMapper bean = ac.getBean(FMapper.class);
        System.out.println(dataSource);
    }

    @PostConstruct
    public void test(){
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        for(String str : beanDefinitionNames){
            System.out.print(str);
            System.out.print("--");
            System.out.println(ac.getBean(str));
        }
    }

    @Autowired
    private ApplicationContext ac;
    @RequestMapping("/getBean/{beanName}")
    public void beans(@PathVariable String beanName){

        Object bean = ac.getBean(beanName);

        System.out.println(bean);
    }

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Autowired
    private Properties properties;

    @RequestMapping("/t")
    public void mapper(){
        System.out.println( fMapper.list4("王二麻子","张三") );
    }

    @RequestMapping("ttt")
    public void ssss(){
        System.out.println(1);
    }

    @Autowired
    private List<MapperFactoryBean> mapperFactoryBeans;

    @PostConstruct
    public void beanDefine() throws NoSuchFieldException, IllegalAccessException {
        GMapper bean = ac.getBean(GMapper.class);
        System.out.println(bean);
        System.out.println(gMapper);
        System.out.println( bean.equals(gMapper));
        System.out.println( bean.equals(gMapper));
        System.out.println( bean == gMapper);
        System.out.println(1);
    }

    @Autowired
    private Dog dog;
}
